function getQueryParams() {
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    var r = {};
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split('=');
        r[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
    }
    return r;
}

function get_root_path(file) {
  var scripts = document.getElementsByTagName('SCRIPT');
  var path = '';
  if(scripts && scripts.length>0) {
    for(var i in scripts) {
      if(scripts[i].src && scripts[i].src.match(file)) {
        path = scripts[i].src.replace(file, '');
        break;
      }
    }
  }
  return path;
}

function ajax_get(url, cb) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url);
  xhr.onload = function() {
    if (xhr.status === 200) {
        try {
            cb(JSON.parse(xhr.responseText));
        } catch(e) {
            cb({});
        }
    } else {
        cb(xhr.status);
    }
  };
  xhr.send();
}

function getWxUserinfo(appid) {
  if( navigator.userAgent.toLowerCase().indexOf('micromessenger') > -1 || typeof navigator.wxuserAgent !== "undefined" ) {
    var params = getQueryParams();
    if(params.openid) {
      // 閰嶇疆 jsapi
      configWxShare(appid);
      return params;
    } else {
      window.location.href = '/helper/wx/userinfo?appid='+appid;
      return false;
    }
  } else {
    return false;
  }
}

function configWxShare() {
  var url = '/home/weixinshare?uri='+encodeURIComponent(window.location.href);
  ajax_get(url, function(data) {
    if(data && data.signature) {
      wx.config({
		   debug: true,
        appId     : data.data.appID,
        timestamp : data.data.time,
        nonceStr  : data.noncestr,
        signature : data.token,
        jsApiList : ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'chooseImage', 'openLocation', 'getLocation', 'closeWindow', 'scanQRCode']
      });
    }
  });
}

function setWxShare(title, desc, link, imgurl) {
  wx.ready(function() {
    wx.onMenuShareTimeline({
      title   : title,
      link    : link,
      imgUrl  : imgurl,
      success : function () {
        if(typeof(share_callback) == "function") {
          share_callback('timeline');
        }
      },
      cancel  : function () {
      }
    });

    wx.onMenuShareAppMessage({
      title   : title,
      desc    : desc,
      link    : link,
      imgUrl  : imgurl,
      type    : 'link',
      dataUrl : '',
      success : function () {
        if(typeof(share_callback) == "function") {
          share_callback('msg');
        }
      },
      cancel  : function () {
      }
    });

    wx.onMenuShareQQ({
      title   : title,
      desc    : desc,
      link    : link,
      imgurl  : imgurl,
      success : function () {
        if(typeof(share_callback) == "function") {
          share_callback('qq');
        }
      },
      cancel  : function () {
      }
    });

    wx.onMenuShareWeibo({
      title   : title,
      desc    : desc,
      link    : link,
      imgurl  : imgurl,
      success : function () {
        if(typeof(share_callback) == "function") {
          share_callback('weibo');
        }
      },
      cancel  : function () {
      }
    });

    return;
  });
}

function setWxShareNoApi(title, desc, link, imgurl) {
  document.title = title;
  document.description = desc;

  var userAgent = navigator.userAgent || navigator.vendor || window.opera;
  if (/android/i.test(userAgent)) {
    document.documentURI = link;
  } else {
    window.history.pushState("weixin-share-url", "weixinshare", link);
  }

  document.write('<div style="display:none"><img src="'+imgurl+'"/></div>')
}